/*
 * 描          述:  <描述>
 * 修  改   人:  Administrator
 * 修改时间:  2014年5月11日
 * <修改描述:>
 */
package com.tx.component.file.context;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;

import com.tx.component.file.catalog.VitualFileCatalog;
import com.tx.component.file.catalog.VitualFileCatalogFactory;
import com.tx.component.file.configurator.VitualFileCatalogConfigurator;
import com.tx.core.exceptions.util.AssertUtils;

/**
 * 文件容器构建器<br/>
 *
 * @author Administrator
 * @version [版本号, 2014年5月11日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class FileContextBuilder extends FileContextConfigurator {
    
    /** 文件目录映射 */
    protected Map<String, VitualFileCatalog> catalogMap;
    
    /**
     * @throws Exception
     */
    @Override
    protected final void doBuild() throws Exception {
        this.catalogMap = new HashMap<>();
        
        Collection<VitualFileCatalogConfigurator> configurators = applicationContext
                .getBeansOfType(VitualFileCatalogConfigurator.class)
                .values();
        if (CollectionUtils.isEmpty(configurators)) {
            return;
        }
        
        //根据配置器获取对应的实例
        for (VitualFileCatalogConfigurator c : configurators) {
            VitualFileCatalog vfc = buildVitualFileCatalog(c);
            this.catalogMap.put(vfc.getCatalog().toUpperCase(), vfc);
        }
    }
    
    /**
     * 获取对应的目录<br/>
     * <功能详细描述>
     * @param catalog
     * @return [参数说明]
     * 
     * @return VitualFileCatalog [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    protected VitualFileCatalog doGetCatalog(String catalog) {
        AssertUtils.notEmpty(catalog, "catalog is empty.");
        
        VitualFileCatalog vfc = this.catalogMap.get(catalog.toUpperCase());
        return vfc;
    }
    
    /**
     * 构建默认的基础数据业务类<br/>
     * <功能详细描述>
     * @param type [参数说明]
     * 
     * @return void [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    private VitualFileCatalog buildVitualFileCatalog(
            VitualFileCatalogConfigurator configurator) {
        BeanDefinitionBuilder builder = null;
        Class<?> vfcFactoryType = VitualFileCatalogFactory.class;
        
        builder = BeanDefinitionBuilder.genericBeanDefinition(vfcFactoryType);
        builder.addPropertyValue("configurator", configurator);
        
        //注册，生成，并返回业务层
        String beanName = generateCatalogBeanName(configurator.catalog());
        if (!beanDefinitionRegistry.containsBeanDefinition(beanName)) {
            logger.debug(
                    "动态注入虚拟文件目录: catalog:{} beanName:{} Type:com.tx.component.file.catalog.VitualFileCatalogFactory",
                    configurator.catalog(),
                    beanName);
            
            beanDefinitionRegistry.registerBeanDefinition(beanName,
                    builder.getBeanDefinition());
        }
        //利用有参构造函数
        VitualFileCatalog catalog = (VitualFileCatalog) applicationContext
                .getBean(beanName);
        return catalog;
    }
    
    /**
     * 生成对应的业务层Bean名称<br/>
     * <功能详细描述>
     * @param type
     * @return [参数说明]
     * 
     * @return String [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    private String generateCatalogBeanName(String catalog) {
        String beanName = "file.catalog." + catalog;
        return beanName;
    }
}
